use account_book::AccountBook;
use memus::{show_main_menu, show_query_menu};

use crate::{
    account_item::AccountItem,
    common::{EXPAND, INCOME},
    memus::show_account_menu,
    read_input::{
        do_enter_key_continue, do_read_amount, do_read_detail, quit_confirm, read_menu_selection,
    },
};

mod account_book;
mod account_item;
mod common;
mod memus;
mod operations;
mod read_input;

fn main() {
    // 1、加载文件中的账目数据
    let mut book = AccountBook::load_from_file();
    let mut quit: bool = false;
    loop {
        if quit {
            break;
        }
        // 2、显示主菜单
        show_main_menu();
        // 3、读取键盘选择，并做合法性校验
        let key: char = read_menu_selection(3);
        match key {
            '1' => {
                // 操作记账菜单
                operate_account_menu(&mut book);
            }
            '2' => {
                operate_query_menu(&mut book);
            }
            '3' => {
                println!("\n确认退出？(Y/N): ");
                if quit_confirm() {
                    quit = true;
                }
            }
            _ => {}
        }
        println!("")
    }
}

fn operate_query_menu(book: &mut AccountBook) {
    loop {
        show_query_menu();
        let key: char = read_menu_selection(4);
        println!("");
        match key {
            '1' => {
                for item in &book.items {
                    println!("{} {} {}", item.item_type, item.amount, item.detail);
                }
                do_enter_key_continue();
            }
            '2' => {
                for item in &book.items {
                    if item.item_type.eq(INCOME) {
                        println!("{} {} {}", item.item_type, item.amount, item.detail);
                    }
                }
                do_enter_key_continue();
            }
            '3' => {
                for item in &book.items {
                    if item.item_type.eq(EXPAND) {
                        println!("{} {} {}", item.item_type, item.amount, item.detail);
                    }
                }
                do_enter_key_continue();
            }
            '4' => {
                break;
            }
            _ => {}
        }
    }
}

// 操作记账页面
fn operate_account_menu(book: &mut AccountBook) {
    loop {
        show_account_menu();
        let key: char = read_menu_selection(3);
        match key {
            '1' => {
                println!("收入金额：");
                let amount = do_read_amount();
                println!("输入备注：");
                let detail = do_read_detail();

                let item: AccountItem = AccountItem::new(INCOME.to_string(), amount, detail);
                book.do_account(item);
                do_enter_key_continue();
            }
            '2' => {
                println!("支出金额：");
                let amount = do_read_amount();
                println!("输入备注：");
                let detail = do_read_detail();

                let item: AccountItem = AccountItem::new(EXPAND.to_string(), amount, detail);
                book.do_account(item);
                do_enter_key_continue();
            }
            '3' => {
                break;
            }
            _ => {}
        }
    }
}
